package com.atguigu.servicebase.exception;

import com.atguigu.commonutils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.stream.Stream;

/**
 *      统一异常处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody //返回json数据
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.error().message("执行了全局异常处理");
    }

    //指定异常处理
    @ResponseBody //返回json数据
    @ExceptionHandler(ArithmeticException.class)
    public Result error(ArithmeticException arithmetic) {
        arithmetic.printStackTrace();
        return Result.error().message("执行了算术异常");
    }

    //自定义类异常
    @ResponseBody //返回json数据
    @ExceptionHandler(GuliException.class)
    public Result error(GuliException e) {
        log.error("异常信息:==>" + e.getMessage());
        e.printStackTrace();
        return Result.error().code(e.getCode()).message(e.getMsg());
    }
}






